$(document).ready(function() {
    //多选下拉框
    var ctx = $("#ctx").val(); //获取服务器地址
    var accountSysType = $("#sessionoUserSysType").val();
    var accountAgentId = $("#sessionagentId").val();
    var accountAdvertiserId = $("#sessionadvertiserId").val();

    //请选择维度
    if ($("#main_dimensionul").html()) {
        $("#dimension_son_ul").hide();
        var url = ctx + "/advertiser/main/ad/report/advertiser-report/dimensionlist";
        getSelectData1("dimensionul_span", "dimension_son_ul", "dimensionul_box", -1, "请选择维度", url, null, "dimension");
        $('ul#dimension_son_ul li').live("click", function() {
            $(this).parents('li').find('span').text($(this).text());
            $("#dimensionId").val($(this).attr("value"));
            $("#dimensionName").val($(this).text());
            $('#dimension_son_ul').hide(); //ul隐藏
        });
    }

    function getSelectData1(target_spanId, target_sonulId, target_boxId, defaultValue, defaultName, url, paramValue, tabId) {
        $("#" + target_sonulId).append("<li value=" + defaultValue + ">" + defaultName + "</li>");
        $.getJSON(url, function(data) {
            if (tabId != null && tabId == "dimension") {
                $.each(data, function(key, val) {
                    $("#dimension_son_ul").append('<li value=' + val.id + '>' + val.name + '</li>');
                });
            } else if (tabId != null && tabId == "operator") {
                var data_1 = data[paramValue];
                $.each(data_1, function(key, val) {
                    $("#" + target_sonulId).append('<li class="selectop2" value=' + val.id + '>' + val.companyName + '</li>');
                });
            } else {
                var data_1 = data[paramValue];
                $.each(data_1, function(key, val) {
                    $("#" + target_sonulId).append('<li value=' + val.value + '>' + val.label + '</li>');
                });
            }
        });
        $("#" + target_spanId).on("click", function() {
            if ($("#" + target_sonulId).height() > 210) {
                $("#" + target_sonulId).addClass("son_ul_change");
            }
            $("#" + target_sonulId).show(); //ul显示
        });
        $("#" + target_boxId).mouseleave(function() {
            $("#" + target_sonulId).hide(); //ul隐藏
            $("#" + target_sonulId).removeClass("son_ul_change");
        });
    }

    //获取广告主，订单，活动，策略
    //最先处理广告主下拉框：若后台系统登录（systype=1）账号登陆,则显示全部代理商的全部广告主；若是需求方（systype=3）账号代理商登陆，显示该代理商的所有广告主；
    if (accountSysType == 1) { //后台系统
        getData(ctx + "/main/commoncombobox/advertiserCombobox", "", "advertiser");
    } else if (accountSysType == 3 && accountAgentId > 0) { //需求方系统
        if (accountAdvertiserId == 0) { //代理商登陆
            getData(ctx + "/main/commoncombobox/advertiserCombobox?id=" + accountAgentId, "", "advertiser");
        } else if (accountAdvertiserId > 0) { //广告主登陆
            getData(ctx + "/main/commoncombobox/advertiserIdCombobox?id=" + accountAdvertiserId, "", "advertiser");
        }
    }

    if ($("#advertiserId").val() != "") {
        $("#navDiv_ul").append('<li><i class="icon-angle-right"></i></li><li><a class="crumb-select-item" title="' + $("#advertiserName").val() + '"><b>广告主：</b><em>' + $("#advertiserName").val() + '</em><i></i></a></li>');
        $(".selector.operator").hide();
        getData(ctx + "/main/commoncombobox/orderCombobox?id=" + $("#advertiserId").val(), $("#advertiserId").val(), "order");
    }

    if ($("#orderId").val() != "") {
        $("#navDiv_ul").append('<li><i class="icon-angle-right"></i></li><li><a class="crumb-select-item" title="' + $("#orderName").val() + '"><b>订单：</b><em>' + $("#orderName").val() + '</em><i></i></a></li>');
        $(".selector.size").hide();
        getData(ctx + "/main/commoncombobox/adgroupCombobox?id=" + $("#orderId").val(), $("#orderId").val(), "adGroup");
    }
    if ($("#adGroupId").val() != "" && $("#adGroupId").val() != undefined) {
        $("#navDiv_ul").append('<li><i class="icon-angle-right"></i></li><li><a class="crumb-select-item" title="' + $("#adGroupName").val() + '"><b>策略：</b><em>' + $("#adGroupName").val() + '</em><i></i></a></li>');
        $(".selector.position").hide();
    }
    $(".selector .sl-wrap.single .valueList li").live("click", function() {
        var parentObj = $(this).parents(".selector");
        var id = $(this).attr("value").toString();
        if (id != "") {
            if (id == 0) { //选择全部
                var ids = "";
                parentObj.find(".sl-wrap.single .valueList li").each(function(i) {
                    ids += $(this).attr("value") + ",";
                });
                id = ids.substring(2, ids.length - 1);
            }
            var type = getType(parentObj, id, $(this).find("a").text());
            var html = '<li><i class="icon-angle-right"></i></li><li><a class="crumb-select-item" title="' + $(this).find("a").text() + '"><b>' + type + '：</b><em>' + $(this).find("a").text() + '</em><i></i></a></li>';
            $("#navDiv_ul").append(html);
            parentObj.hide();
        }
        return false;
    });
    $("#navDiv_ul li a.crumb-select-item i").live("click", function() {
        var parentObj = $(this).parents("a.crumb-select-item");
        if (parentObj.find("b").text().indexOf("广告主") != -1) {
            $("#advertiserId").val("");
            $("#orderId").val("");
            $("#adGroupId").val("");
            $("#advertiserName").val("");
            $("#orderName").val(name);
            $("#adGroupName").val("");
            $(".selector.operator").show();
            $(".selector.size").show();
            $(".selector.type").show();
            $(".selector.position").show();
            $(".selector .sl-wrap").removeClass("multiple").addClass("single");
            $(".selector.operator .sl-wrap .valueList li:first").show();
            $(".selector.size .sl-wrap .valueList li:first").show();
            $(".selector.type .sl-wrap .valueList li:first").show();
            $(".selector.position .sl-wrap .valueList li:first").show();
            $(".selector.size .sl-wrap .valueList li.out").remove();
            $(".selector.type .sl-wrap .valueList li.out").remove();
            $(".selector.position .sl-wrap .valueList li.out").remove();
            $(".selector.operator .sl-wrap .valueList li").removeClass("selected");
            $(".selector .btnsConfirm").addClass("disabled");
        } else if (parentObj.find("b").text().indexOf("订单") != -1) {
            $("#orderId").val("");
            $("#adGroupId").val("");
            $("#orderName").val("");
            $("#adGroupName").val("");
            $(".selector.size").show();
            $(".selector.type").show();
            $(".selector.position").show();
            $(".selector .sl-wrap").removeClass("multiple").addClass("single");
            $(".selector.size .sl-wrap .valueList li:first").show();
            $(".selector.type .sl-wrap .valueList li:first").show();
            $(".selector.position .sl-wrap .valueList li:first").show();
            $(".selector.type .sl-wrap .valueList li.out").remove();
            $(".selector.position .sl-wrap .valueList li.out").remove();
            $(".selector.size .sl-wrap .valueList li").removeClass("selected");
            $(".selector.operator").nextAll(".selector").find(".btnsConfirm").addClass("disabled");
        } else if (parentObj.find("b").text().indexOf("策略") != -1) {
            $("#adGroupId").val("");
            $("#adGroupName").val("");
            $(".selector.position").show();
            $(".selector .sl-wrap").removeClass("multiple").addClass("single");
            $(".selector.position .sl-wrap .valueList li:first").show();
            $(".selector.position .sl-wrap .valueList li").removeClass("selected");
            $(".selector.position .btnsConfirm").addClass("disabled");
        }
        $(this).parents("li").nextAll().remove();
        $(this).parents("li").prev().remove();
        $(this).parents("li").remove();
        return false;
    });

    //多选
    $(".extMultiple").click(function() {
        var parentObj = $(this).parents(".selector");
        $(".selector .sl-wrap").removeClass("multiple").addClass("single");
        parentObj.find(".sl-wrap").removeClass("single").addClass("multiple");
        parentObj.find(".sl-wrap.multiple .valueList li:first").hide();
    });
    $(".selector .sl-wrap.multiple .valueList li").live("click", function() {
        var parentObj = $(this).parents(".selector");
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
        } else {
            $(this).addClass("selected");
        }
        if ($(this).parent(".valueList").find("li.selected").length > 0) {
            parentObj.find(".btnsConfirm").removeClass("disabled");
        } else {
            parentObj.find(".btnsConfirm").addClass("disabled");
        }
    });

    //确定
    $(".btnsConfirm").click(function() {
        var parentObj = $(this).parents(".selector");
        var ids = "";
        var names = "";
        parentObj.find(".sl-wrap.multiple .valueList li.selected").each(function(i) {
            ids += $(this).attr("value") + ",";
            names += $(this).find("a").text() + "、";
        });
        if (ids != "") {
            ids = ids.substring(0, ids.length - 1);
            names = names.substring(0, names.length - 1);
            var type = getType(parentObj, ids, names);
            var html = '<li><i class="icon-angle-right"></i></li><li><a class="crumb-select-item" title="' + names + '"><b>' + type + '：</b><em>' + names + '</em><i></i></a></li>';
            $("#navDiv_ul").append(html);
            parentObj.hide();
        }
        return false;
    });

    //取消
    $(".btnsCancel").click(function() {
        var parentObj = $(this).parents(".selector");
        parentObj.find(".sl-wrap").removeClass("multiple").addClass("single");
        parentObj.find(".sl-wrap .valueList li").removeClass("selected");
        parentObj.find(".sl-wrap .btnsConfirm").addClass("disabled");
        parentObj.find(".sl-wrap .valueList li:first").show();
    });
});

function getData(url, ids, tab) {
    $.ajax({
        async: false,
        type: "POST",
        url: url,
        data: {
            'ids': ids
        },
        success: function(data) {
            if (tab == "advertiser") {
                for (var i = 0; i < data.advertiserList.length; i++) {
                    $(".selector.operator .valueList").append('<li class="out" value=' + data.advertiserList[i].id + '><a><i></i>' + data.advertiserList[i].name + '</a></li>');
                }
            } else if (tab == "order") {
                for (var i = 0; i < data.orderList.length; i++) {
                    $(".selector.size .valueList").append('<li class="out" value=' + data.orderList[i].id + '><a><i></i>' + data.orderList[i].name + '</a></li>');
                }
            } else if (tab == "adGroup") {
                for (var i = 0; i < data.adgroupList.length; i++) {
                    $(".selector.position .valueList").append('<li class="out" value=' + data.adgroupList[i].id + '><a><i></i>' + data.adgroupList[i].adGroupName + '</a></li>');
                }
            }
        }
    });
}

function getType(obj, id, name) {
    var type = "";
    if (obj.hasClass("operator")) {
        type = "广告主";
        $("#advertiserId").val(id);
        $("#advertiserName").val(name);
        getData($("#ctx").val() + "/main/commoncombobox/orderCombobox?id=" + $("#advertiserId").val(), $("#advertiserId").val(), "order");
    } else if (obj.hasClass("size")) {
        type = "订单";
        $("#orderId").val(id);
        $("#orderName").val(name);
        getData($("#ctx").val() + "/main/commoncombobox/adgroupCombobox?id=" + $("#orderId").val(), $("#orderId").val(), "adGroup");
    } else if (obj.hasClass("position")) {
        type = "策略";
        $("#adGroupId").val(id);
        $("#adGroupName").val(name);
    }
    return type;
}